package org.sun.web.controller;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sun.basic.dao.IChartItemDao;
import org.sun.basic.model.ChartItem;
import org.sun.web.util.DataUtil;

@Controller
public class DashboardController {
	
	@Inject
	private IChartItemDao chartItemDao;

	@RequestMapping(value="index/dashboard",method=RequestMethod.GET)
	public static String toDashboardPg(){
		
		return "index/dashboard";
	}
	
	@RequestMapping(value="dashboard/save",method=RequestMethod.POST)
	public @ResponseBody String saveItem(@RequestBody ChartItem item){
		if(item==null)return "error";
		try {
			if(chartItemDao.load(item.getId())==null)
			   chartItemDao.add(item);
			else{
				chartItemDao.delete(item.getId());
				chartItemDao.add(item);
			} 
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return null;
	}
	@RequestMapping(value="dashboard/delete",method=RequestMethod.POST)
	public @ResponseBody String deleteItem(@RequestBody String id){
		if(id==null)return "error";
		ChartItem ci=chartItemDao.load(id);
		if(ci!=null)chartItemDao.delete(id);
		return null;
	}
	@RequestMapping(value="dashboard/load",method=RequestMethod.POST)
	public @ResponseBody List<ChartItem> loadItem(){
		List<ChartItem> list=chartItemDao.list("from ChartItem");
		if(list.size()<1)return null;
		return list;
	}
}
